package com.gaojiabao.panghu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaojiabao.panghu.constant.ResultCodeEnum;
import com.gaojiabao.panghu.data.dto.ConditionDto;
import com.gaojiabao.panghu.data.pojo.Store;
import com.gaojiabao.panghu.mapper.StoreMapper;
import com.gaojiabao.panghu.service.StoreService;
import com.gaojiabao.panghu.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 * @Author: gaojiabao
 * @Date: 2024/11/6 8:42
 */
@Service
public class StoreServiceImpl implements StoreService {
    @Resource
    private StoreMapper storeMapper;

    //分页 + 条件查询门店列表
    @Override
    public Result<?> getStorePageList(Integer currentPage,
                                      Integer pageSize,
                                      ConditionDto conditionDto,
                                      Integer deletedState) {
        if (currentPage == 0 || pageSize == 0) {
            return Result.build(ResultCodeEnum.PATH_PARAM_ERROR);
        }

        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
        //条件查询——门店名称，门店联系方式，门店创建时间范围
        if (null != conditionDto) {
            if (!conditionDto.getKeyWord().isBlank())
                queryWrapper.like(Store::getStoreName, conditionDto.getKeyWord());
            if (!conditionDto.getPhone().isBlank())
                queryWrapper.like(Store::getStorePhone, conditionDto.getPhone());
            if (conditionDto.getPreDate() != null)
                queryWrapper.ge(Store::getCreateTime, conditionDto.getPreDate());
            if (conditionDto.getPostData() != null)
                queryWrapper.le(Store::getCreateTime, conditionDto.getPostData());
        }
        //条件查询——是否删除
        if (null != deletedState ) {
            queryWrapper.eq(Store::getStatus, deletedState);
        }
        //id降序排列
        queryWrapper.orderByDesc(Store::getId);

        //分页+条件查询门店列表
        Page<Store> selectPage = new Page<>(currentPage, pageSize);
        Page<Store> storePageList = storeMapper.selectPage(selectPage, queryWrapper);

        return Result.ok(storePageList);
    }

    //逻辑删除门店
    @Override
    @Transactional
    public Result<?> deleteStoreById(Integer id) {
        //判断该id的门店是否存在
        if (!existStore(id)) {
            return Result.build(ResultCodeEnum.STORE_NO_ID);
        }

        //添加修改时间
        LambdaUpdateWrapper<Store> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Store::getId, id)
                .set(Store::getDeleteTime, LocalDateTime.now());
        storeMapper.update(updateWrapper);

        int row = storeMapper.deleteById(id);
        if (row > 0) {
            return Result.ok();
        } else {
            return Result.build(ResultCodeEnum.RECORD_DELETE_ERROR);
        }
    }

    //新增门店
    @Override
    @Transactional
    public Result<?> addStore(Store store) {
        //新增门店创建时间
        store.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));

        int row = storeMapper.insert(store);
        if (row > 0) {
            return Result.ok();
        } else {
            return Result.build(ResultCodeEnum.RECORD_ADD_ERROR);
        }
    }

    //id -> 获取门店信息
    @Override
    public Result<?> getStoreById(Integer id) {
        Store store = storeMapper.selectById(id);
        if (null == store)
            return Result.build(ResultCodeEnum.STORE_NO_ID);
        return Result.ok(store);
    }

    //编辑门店
    @Override
    public Result<?> updateStore(Store store) {
        //sql条件
        LambdaUpdateWrapper<Store> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Store::getId, store.getId())
                .set(Store::getStoreImg, store.getStoreImg())
                .set(Store::getStoreName, store.getStoreName())
                .set(Store::getStorePhone, store.getStorePhone())
                .set(Store::getFullAddress, store.getFullAddress())
                .set(Store::getSimpleAddress, store.getSimpleAddress())
                .set(Store::getProvince, store.getProvince())
                .set(Store::getCity, store.getCity())
                .set(Store::getDistrict, store.getDistrict())
                .set(Store::getOpenTime, store.getOpenTime())
                .set(Store::getCloseTime, store.getCloseTime())
                .set(Store::getCommunityId, store.getCommunityId())
                .set(Store::getStatus,store.getStatus())
                .set(Store::getCommunityId,store.getCommunityId());

        //判断是否修改成功
        int row = storeMapper.update(updateWrapper);
        if (row > 0) {
            return Result.ok();
        } else {
            return Result.build(ResultCodeEnum.RECORD_UPDATE_ERROR);
        }
    }

    /*=========================================================*/

    //提取方法——检查该id的门店是否存在
    public Boolean existStore(Integer id) {
        Store store = storeMapper.selectById(id);
        return store != null;
    }
}
